import { MenuType } from '@/types/menu';
import { useCookies } from '@vueuse/integrations/useCookies';

const cookie = useCookies();

export const getToken = () => cookie.get('token');
export const setToken = (token: string) => cookie.set('token', token);
export const removeToken = () => cookie.remove('token');

export const getUser = () => cookie.get('user');
export const setUser = (user: any) => cookie.set('user', user);
export const removeUser = () => cookie.remove('user');

export const getMenuTags = () => {
  const menuTags: MenuType[] = cookie.get('menuTags') ?? [];
  if (!menuTags.find(t => t.id === '1-1')) {
    menuTags.unshift({
      id: '1-1',
      name: '首页',
      path: '/manager/home',
      icon: 'mdi-home',
      close: true,
    });
  }
  return menuTags;
};

export const addMenuTags = (menuTag: MenuType) => {
  const menu: MenuType[] = cookie.get('menuTags') ?? [];
  menu.push(menuTag);
  cookie.set('menuTags', menu);
};

export const removeMenuTags = (id: string) => {
  const menu: MenuType[] = cookie.get('menuTags') ?? [];
  const index = menu.filter(t => t.id !== id);
  cookie.set('menuTags', index);
};
